package com.xxh.market.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.xxh.market.dao.BookDao;
import com.xxh.market.entity.Book;
import com.xxh.market.entity.Books;
import com.xxh.market.utils.jdbcUtils;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {

    @Override
    public List<Book> getBooksByTypeId(int typeId) {
        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        List<Book> books=new ArrayList<Book>();
        try {
            conn= jdbcUtils.getConnection();
            String sql="select id,bname,author,price,press from book where typeid=?";
            psmt=conn.prepareStatement(sql);
            psmt.setInt(1, typeId);
            rs=psmt.executeQuery();
            while(rs.next()) {
                int id=rs.getInt(1);
                String bname=rs.getString(2);
                String author=rs.getString(3);
                double price=rs.getDouble(4);
                String press=rs.getString(5);
                Book book = new Book();
                book.setBname(bname);
                book.setAuthor(author);
                book.setPress(press);
                book.setId(id);
                book.setPrice(price);
                books.add(book);
            }
            return books;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            jdbcUtils.close(rs, psmt, conn);
        }

        return null;
    }

    @Override
    public List<Book> getBooksByName(String bookName) {

        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        List<Book> books=new ArrayList<Book>();
        try {
            conn= jdbcUtils.getConnection();
            String sql="select id,bname,author,price,press from book where bname like ?";
            psmt=conn.prepareStatement(sql);
            psmt.setString(1, "%"+bookName+"%");
            rs=psmt.executeQuery();
            while(rs.next()) {
                int id=rs.getInt(1);
                String bname=rs.getString(2);
                String author=rs.getString(3);
                double price=rs.getDouble(4);
                String press=rs.getString(5);
                Book book = new Book();
                book.setBname(bname);
                book.setAuthor(author);
                book.setPress(press);
                book.setId(id);
                book.setPrice(price);
                books.add(book);
            }
            return books;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            jdbcUtils.close(rs, psmt, conn);
        }
//
//        try {
//            List<Entity> list = Db.use().query("select id,bname,author,price,press from book where bname like ?", "%" + bookName + "%");
//
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
        return null;
    }

    @Override
    public Book getBookByID(Integer bid) {
        try {
            List<Entity> entities = Db.use().query("select * from book where id =?", bid);
            Entity entity = entities.get(0);
            Book book = new Book();
            book.setPrice(entity.getDouble("price"));
            book.setId(bid);
            book.setBname(entity.getStr("bname"));
            return book;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
}
